package com.gmlive.common.host.helper;

import android.text.TextUtils;

import com.gmlive.common.host.manager.HostUpdateManager;

import java.util.LinkedHashMap;

/**
 * @作者: 姜志敏
 * @日期: 2019-08-21
 */
public class ApiUrlHelper {

    private LinkedHashMap<String, String> map = new LinkedHashMap<>();

    private ApiUrlHelper() {
    }

    private static class SingletonHolder {
        static ApiUrlHelper helper = new ApiUrlHelper();
    }

    public static ApiUrlHelper getInstance() {
        return SingletonHolder.helper;
    }


    public boolean isHostInit() {
        return map != null && !map.isEmpty();
    }

    public void setUpHostMap(LinkedHashMap<String, String> map) {
        this.map = map;
    }

    /**
     * "data": {
     * "App": "https://serviceky.hnyapu.cn",
     * "H5": "https://yph5.hnyapu.cn",
     * "ImageScale": "https://imagescalekyik.hnyapu.cn",
     * "Img2ik": "http://img2kyik.hnyapu.cn",
     * "M4aik": "http://m4akyik.hnyapu.cn",
     * "Main": "https://yp.hnyapu.cn",
     * "Pay": "https://serviceky.hnyapu.cn",
     * "Serviceik": "https://serviceky.hnyapu.cn",
     * "Serviceinfoik": "https://serviceky.hnyapu.cn",
     * "TestApp": "https://testserviceky.hnyapu.cn",
     * "TestH5": "https://testyph5.hnyapu.cn",
     * "TestPay": "https://testserviceky.hnyapu.cn",
     * "Upload": "http://uploadky.hnyapu.cn",
     * "Uploadlog": "http://uploadkyik.hnyapu.cn",
     * "Vocik": "http://vockyik.hnyapu.cn"
     * }
     */

    public String getUrl(String tempUrl) {
        if (TextUtils.isEmpty(tempUrl)) {
            return "";
        }
        if (map == null || map.isEmpty()) {
            return tempUrl;
        }

        String[] split = null;
        if (tempUrl.contains("/")) {
            split = tempUrl.split("/");
        }
        if (split == null || split.length == 0) {
            return tempUrl;
        }
        String target = split[0];
        if (TextUtils.isEmpty(target)) {
            return tempUrl;
        }

        //测试环境-拉去长链接配置
        if (isTest() && tempUrl.contains("/time/getconfig")) {
            return tempUrl.replace(target, getTestInkeHost());
        }

        String host = getEnvironmentHost(target);

        if (TextUtils.isEmpty(host)) {
            return tempUrl;
        }

        return tempUrl.replace(target, host);
    }

    private boolean isTest() {
        return HostUpdateManager.ins().isTest();
    }

    public String getEnvironmentHost(String key) {

        if (key.contains("_HOST")) {
            key = key.replace("_HOST", "");
        }

        if (!isTest()) {
            if (map.containsKey(key)) {
                return map.get(key);
            }
            return "";
        } else {
            String testTarget = "Test" + key;
            if (map.containsKey(testTarget)) {
                return map.get(testTarget);
            } else if (map.containsKey(key)) {
                return map.get(key);
            } else {
                return "";
            }
        }
    }

    /**
     * H5地址
     *
     * @return
     */
    public String getH5Host() {
        return getEnvironmentHost("H5");
    }

    /**
     * 主域名-html
     *
     * @return
     */
    public String getMainHost() {
        return getEnvironmentHost("Main");
    }

    /**
     * 测试环境-拉去长链接配置
     *
     * @return
     */
    private String getTestInkeHost() {
        //TODO 测试环境直接写死
        return "http://serviceinfo.test.inke.cn";
    }


    /**
     * 替换分享url
     *
     * @param url
     * @return
     */
    public String getShareTargetUrl(String url) {
        if (url.contains("https://h5.xiangshengclub.com")) {
            url = url.replace("https://h5.xiangshengclub.com", getH5Host());
        }
        return url;
    }

}
